//
//  KLCrashLoanViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/7/23.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLCrashLoanViewController.h"
#import "KLHelp.h"
#import "KLHttpAPI.h"
#import "HemaConst.h"
#import "Masonry.h"
#import "TSCurrencyTextField.h"
#import "DCPaymentView.h"
#import "JRToast.h"

@interface KLCrashLoanViewController () <UITextFieldDelegate>
{
    UIView *fatherView_1;
    TSCurrencyTextField *numberTF;
    UIButton *certainBtn;
}
@property (nonatomic,strong) NSMutableString *money;
@end

@implementation KLCrashLoanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化导航栏
    [self naveSet];
    //初始化界面
    [self uiset];
}

#pragma mark - 初始化导航栏
-(void)naveSet{
    self.navigationController.navigationBarHidden = NO;
    self.title = @"金额";
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 初始化界面
-(void)uiset{
    self.view.backgroundColor = RGB_UI_COLOR(238, 238, 238);
    
    fatherView_1 = [[UIView alloc]init];
    fatherView_1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:fatherView_1];
    [fatherView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    //金额提示label
    UILabel *numberLabel = [[UILabel alloc]init];
    numberLabel.text = @"金额";
    numberLabel.font = [UIFont systemFontOfSize:18.0f];
    [fatherView_1 addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(fatherView_1).with.offset(0);
    }];
    //输入金额的textfield
    numberTF = [[TSCurrencyTextField alloc]init];
    [numberTF addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    numberTF.borderStyle = UITextBorderStyleNone;
    numberTF.amount = @0.00;
    numberTF.keyboardType = UIKeyboardTypeNumberPad;
    [numberTF becomeFirstResponder];
    [fatherView_1 addSubview:numberTF];
    [numberTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(numberLabel).with.offset(60);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(fatherView_1).with.offset(0);
        make.right.equalTo(fatherView_1).with.offset(-50);
    }];
    
    certainBtn = [[UIButton alloc]init];
    [certainBtn setBackgroundColor:RGB_UI_COLOR(170, 170, 170)];
    [certainBtn setEnabled:NO];
    certainBtn.layer.masksToBounds = YES;
    certainBtn.layer.cornerRadius = 5.0f;
    [certainBtn setTitle:@"确定" forState:UIControlStateNormal];
    [certainBtn addTarget:self action:@selector(certainCash:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:certainBtn];
    [certainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(fatherView_1).offset(75);
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
    
    UILabel *tipsLabel = [[UILabel alloc]init];
    tipsLabel.text = @"每次限额1000.00元人民币整！\n提现会收取0.6%的手续费！\n节假日不受理提现！\n未认证用户无法提现";
    tipsLabel.textColor = RGB_UI_COLOR(255, 153, 0);
    tipsLabel.textAlignment = NSTextAlignmentCenter;
    tipsLabel.numberOfLines = 0;
    tipsLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(certainBtn).offset(70);
        make.centerX.equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(85);
    }];
}

#pragma mark - uitextfield的实时显示方法
-(void)textFieldEditChanged:(TSCurrencyTextField *)textField{
    NSString *text = [textField.text substringFromIndex:1];//不同的iphone截断距离不一样
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray *array = [text componentsSeparatedByString:@","];
    self.money = [NSMutableString string];
    for (NSString *subStr in array) {
        [self.money appendString:subStr];
    }
    if(self.money.doubleValue > 1000 || self.money.doubleValue == 0){
        [self certainBtnNotAllowSelected];
    }else{
        [self certainBtnAllowSelected];
    }
    NSLog(@"金额:%@",self.money);
}

#pragma mark - 确定按钮可选中状态
-(void)certainBtnAllowSelected{
    [certainBtn setBackgroundColor:RGB_UI_COLOR(8, 128, 181)];
    [certainBtn setEnabled:YES];
}

-(void)certainBtnNotAllowSelected{
    [certainBtn setBackgroundColor:RGB_UI_COLOR(170, 170, 170)];
    [certainBtn setEnabled:NO];
}

#pragma mark - 点击确定提现按钮
-(void)certainCash:(UIButton *)sender{
    DCPaymentView *payAlert = [[DCPaymentView alloc]init];
    payAlert.title = @"请输入支付密码";
    payAlert.detail = @"提现";
    payAlert.amount= self.money.doubleValue;
    [payAlert show];
    payAlert.completeHandle = ^(NSString *inputPwd) {
        [KLHttpAPI cashRequestWithApplyfee:self.money paypassword:inputPwd keytype:@"1" completionHandler:^(MKNetworkOperation *operation) {
            NSLog(@"提现返回数据:%@",operation.responseString);
            NSDictionary *dic = operation.responseJSON;
            NSString *isSuccess = [dic objectForKey:@"success"];
            if(isSuccess.integerValue == 1){
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",[dic objectForKey:@"msg"]] forKey:@"feeaccount"];
                //提示多少小时到账
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"提现成功,24个小时内到账" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }else{
                //提示故障
                [JRToast showWithText:[NSString stringWithFormat:@"提现失败,%@",[dic objectForKey:@"msg"]]];
            }
        } errorHandler:^(NSString *error) {
        }];
    };
}
@end
